﻿using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.Model.OrgManage;
using AssetManageSystem.Model.Enum;


namespace AssetManageSystem.ViewModel.AssetManage.AssetInfoVMs
{
    public partial class AssetInfoListVM : BasePagedListVM<AssetInfo_View, AssetInfoSearcher>
    {
        protected override List<GridAction> InitGridAction()
        {
            if (SearcherMode == ListVMSearchModeEnum.MasterDetail)
            {
                return new List<GridAction>();
            }
            else
            {
                return new List<GridAction>
            {
                this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"AssetManage", dialogWidth: 800).SetShowDialog(false).SetIsRedirect(true).SetButtonClass("layui-bg-green"),
                this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-blue"),
                this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.Details, Localizer["Sys.Details"], "AssetManage", dialogWidth: 850).SetButtonClass("btn-col-8"),
                // this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.BatchEdit, Localizer["Sys.BatchEdit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-orange"),
                this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.BatchDelete, Localizer["Sys.BatchDelete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                // this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.Import, Localizer["Sys.Import"], "AssetManage", dialogWidth: 800).SetButtonClass("btn-col-4"),
                this.MakeStandardAction("AssetInfo", GridActionStandardTypesEnum.ExportExcel, Localizer["Sys.Export"], "AssetManage").SetButtonClass("btn-col-4"),
                this.MakeAction("AssetInfo","ReSetStatus","修复","更改状态",GridActionParameterTypesEnum.SingleId,"AssetManage",dialogWidth: 800)
                    .SetShowInRow(true).SetHideOnToolBar(true).SetButtonClass("btn-col-1").SetBindVisiableColName("sta"),

                };
            }
        }


        protected override IEnumerable<IGridColumn<AssetInfo_View>> InitGridHeader()
        {
            if (SearcherMode == ListVMSearchModeEnum.MasterDetail)
            {
                return new List<GridColumn<AssetInfo_View>>{
                this.MakeGridHeader(x => x.AsName),
                this.MakeGridHeader(x => x.AsCode),
                this.MakeGridHeader(x =>x.AssetModels),
                this.MakeGridHeader(x => x.BuyTime),
                this.MakeGridHeader(x => x.UserName_view),
                this.MakeGridHeader(x => x.DepName_view),
                this.MakeGridHeader(x => x.Status).SetForeGroundFunc((a)=>{
                    if(a.Status==AsStatusEnum.OnUse) {return "#1E9FFF";}
                    else if(a.Status==AsStatusEnum.NoUse){return "#009688";}
                    else if(a.Status==AsStatusEnum.Scrap){return "#ed4014";}
                    else if(a.Status==AsStatusEnum.Borrow){return "#FF5722";}
                    else if(a.Status==AsStatusEnum.Repair){return "#2F4056";}
                    else if(a.Status==AsStatusEnum.Donation){return "#FFB800";}
                    return "";
                }),
                };
            }
            else
            {
                return new List<GridColumn<AssetInfo_View>>{
                this.MakeGridHeader(x => x.AsName),
                this.MakeGridHeader(x => x.AsCode),
                this.MakeGridHeader(x =>x.AssetModels),
                this.MakeGridHeader(x => x.BuyTime),
                this.MakeGridHeader(x => x.UserName_view),
                this.MakeGridHeader(x => x.DepName_view),
                this.MakeGridHeader(x => x.Status).SetForeGroundFunc((a)=>{
                    if(a.Status==AsStatusEnum.OnUse) {return "#1E9FFF";}
                    else if(a.Status==AsStatusEnum.NoUse){return "#009688";}
                    else if(a.Status==AsStatusEnum.Scrap){return "#ed4014";}
                    else if(a.Status==AsStatusEnum.Borrow){return "#FF5722";}
                    else if(a.Status==AsStatusEnum.Repair){return "#2F4056";}
                    else if(a.Status==AsStatusEnum.Donation){return "#FFB800";}

                    return "";
                }),
                this.MakeGridHeader(x => x.UseDepId).SetHide(true),
                this.MakeGridHeader(x => x.UsePeopleId).SetHide(true),
                this.MakeGridHeader(x =>"sta").SetHide(true).SetFormat((a,b)=>{
                    if(a.Status == AsStatusEnum.Repair)
                    {
                        return "true";
                    }
                    return "false";
                }),
                this.MakeGridHeaderAction(width: 250)
                };
            }
        }

        public override IOrderedQueryable<AssetInfo_View> GetSearchQuery()
        {
            var query = DC.Set<AssetInfo>()
                .CheckContain(Searcher.Ids,x=>x.ID.ToString())
                .CheckContain(Searcher.AsStatus,x=>x.Status.ToString())
                .CheckContain(Searcher.AsName, x => x.AsName)
                .CheckEqual(Searcher.TypeId, x => x.TypeId)
                .CheckBetween(Searcher.BuyTime?.GetStartTime(), Searcher.BuyTime?.GetEndTime(), x => x.BuyTime, includeMax: false)
                .CheckEqual(Searcher.UsePeopleId, x => x.UsePeopleId)
                .CheckEqual(Searcher.UseDepId, x => x.UseDepId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckEqual(Searcher.Id, x => x.ID)
                .Select(x => new AssetInfo_View
                {
                    ID = x.ID,
                    AsName = x.AsName,
                    AsCode = x.AsCode,
                    BuyTime = x.BuyTime,
                    AssetModels = x.AssetModels,
                    UserName_view = x.UsePeople.UserName,
                    DepName_view = x.UseDep.DepName,
                    Status = x.Status,
                    UseDepId = x.UseDep.ID,
                    UsePeopleId= x.UsePeopleId
                })
                .OrderBy(x => x.ID);
            return query;
        }

        public override IOrderedQueryable<AssetInfo_View> GetSelectorQuery()
        {
            var query = DC.Set<AssetInfo>()
                .Where(x => x.Status != Model.Enum.AsStatusEnum.Scrap)
                .CheckContain(Searcher.AsName, x => x.AsName)
                .CheckEqual(Searcher.TypeId, x => x.TypeId)
                .CheckBetween(Searcher.BuyTime?.GetStartTime(), Searcher.BuyTime?.GetEndTime(), x => x.BuyTime, includeMax: false)
                .CheckEqual(Searcher.UsePeopleId, x => x.UsePeopleId)
                .CheckEqual(Searcher.UseDepId, x => x.UseDepId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckEqual(Searcher.Id, x => x.ID)
                .CheckContain(Searcher.Ids, x => x.ID.ToString())
                .Select(x => new AssetInfo_View
                {
                    ID = x.ID,
                    AsName = x.AsName,
                    AsCode = x.AsCode,
                    BuyTime = x.BuyTime,
                    AssetModels = x.AssetModels,
                    UserName_view = x.UsePeople.UserName,
                    DepName_view = x.UseDep.DepName,
                    Status = x.Status,
                    UseDepId = x.UseDep.ID,
                    UsePeopleId = x.UsePeopleId
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class AssetInfo_View : AssetInfo
    {
        [Display(Name = "使用人")]
        public String UserName_view { get; set; }
        [Display(Name = "使用部门")]
        public String DepName_view { get; set; }
    }

}
